﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using CafeOnline.Common;
using CafeOnline.Common.Criteria;
using CafeOnline.Domain.Entities;
using CafeOnline.Domain.IRepositories;
using CafeOnline.Helpers;
using CafeOnline.MvcFramework.Grid;
using CafeOnline.MvcFramework.Interfaces;

namespace CafeOnline.Grids {
    [Export(typeof(IGridFactory))]
    [ExportMetadata(Constants.GRID_TYPE_NAME, GridTypeNames.CATEGORY)]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class ProductGroupGrid : GridFactoryBase<IEnumerable<ProductGroup>, ProductGroup, ProductGroupCriteria> {
        [Import]
        private IProductGroupRepository m_repository;
        protected override void Sort(ProductGroupCriteria c, string column, MvcFramework.Grid.SortOrder direction) {
            c.Sorting = new Sorting { Column = column, Direction = (direction == SortOrder.desc) ? SortDirection.Desc : SortDirection.Asc };

        }

        protected override void Paging(ProductGroupCriteria c, int take, int skip) {
            c.Take = take;
            c.Skip = skip;
        }

        protected override IEnumerable<ProductGroup> FindData(ProductGroupCriteria c) {
            return m_repository.GetData(c).ToList();
        }

        protected override int GetTotal(ProductGroupCriteria c) {
            return m_repository.CountData(c);
        }

        protected override IGrid Create(string gridType, string gridId, IEnumerable<IColumnFactory> columns, MvcFramework.Grid.GridCustomSettings gridCustomSettings, MvcFramework.Grid.GridOptions options) {
            return new GridBase(gridType, gridId, columns, gridCustomSettings);
        }



    }
    [ColumnMetadataAttribute("categoryId", SortingName = "ProductGroupId", GridType = GridTypeNames.CATEGORY, Order = 1, Label = "Id")]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class CategoryNameColumn : ExtendedColumnOld<ProductGroup, ProductGroupCriteria> {
        public CategoryNameColumn() {
            SetFetchData((row, dic) => row.ProductGroupId);
            SetFiltering((criteria, term) => {
                int id;
                if (int.TryParse(term, out id)) {
                    criteria.ProductGroupId = id;
                }
            });
            AddFormatting((c) => {
                //c.SetWidth(80);
                c.SetSortable(true);
                c.SetSearch(true);
                c.SetTitle(false);
            });
        }
    }
    [ColumnMetadataAttribute("productGroupName", SortingName = "ProductGroupName", GridType = GridTypeNames.CATEGORY, Order = 2, Label = "Category Name")]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class NameColumn : ExtendedColumnOld<ProductGroup, ProductGroupCriteria> {
        public NameColumn() {
            SetFetchData((row, dic) => row.ProductGroupName);
            SetFiltering((criteria, term) => criteria.CategoryName = term);
            AddFormatting((c) => {
                //c.SetWidth(100);
                c.SetSortable(true);
                c.SetSearch(true);
                c.SetTitle(false);
            });
        }
    }
    [ColumnMetadataAttribute("productGrpupDescription", SortingName = "ProductGroupDesc", GridType = GridTypeNames.CATEGORY, Order = 3, Label = "Description")]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class DescriptionColumn : ExtendedColumnOld<ProductGroup, ProductGroupCriteria> {
        public DescriptionColumn() {
            SetFetchData((row, dic) => row.ProductGroupDesc);
            SetFiltering((x, y) => x.Description = y);
            AddFormatting((c) => {
                //c.SetWidth(100);
                c.SetSortable(true);
                c.SetSearch(true);
                c.SetTitle(false);
            });
        }
    }
    [ColumnMetadataAttribute("ImageUrl", GridType = GridTypeNames.CATEGORY, Order = 4, Label = "Image")]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class ImageColumn : ExtendedColumnOld<ProductGroup, ProductGroupCriteria> {
        public ImageColumn() {
            SetFetchData((row, dic) => {
                ColumnHelpers.SetThumnailFormatter(row.ImageUrl, dic);
                return row.ImageUrl;
            });
            AddFormatting((c) => {
                c.SetSortable(false);
                c.SetSearch(false);
                c.SetTitle(false);
                c.SetFormatter(Formatters.thumnailImageFormatter);
            });
        }
    }
}